package com.orange.core.security.detail;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.ArrayList;
import java.util.Collection;

@Getter
@Setter
@Accessors(chain = true)
public class UserDetail extends User {

    private Integer id;
    private com.orange.core.pojo.entity.user.User user;
    private String token;

    /**
     * @param username 手机号
     */
    public UserDetail(int id, String username, Collection<? extends GrantedAuthority> authorities) {
        super(username, "", authorities);
        this.id = id;
    }

    /**
     * @param username 手机号
     */
    @JsonCreator
    public UserDetail(@JsonProperty(value = "id") int id,
                      @JsonProperty(value = "username") String username,
                      @JsonProperty(value = "enabled") boolean enabled) {
        super(username, "", enabled, true, true, true, new ArrayList<>());
        this.id = id;
    }

    public void authenticationCheck() {
        if (!this.isEnabled()) {
            throw new DisabledException(String.format("当前账号 %s 已禁用", this.getUsername()));
        }
    }
}
